//
//  LCSTextFild.m
//  理财师
//
//  Created by admin on 16/4/21.
//  Copyright © 2016年 admin. All rights reserved.
//

#import "LCSTextFild.h"

@implementation LCSTextFild
{
    UITextField *_txtFild;
    UIButton * _ligBtn;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(NSString *)text
{
    return _txtFild.text;
}
-(void)setText:(NSString *)text
{
    _txtFild.text = text;
}

-(UIColor *)textColor
{
    return _txtFild.textColor;
}
-(void)setTextColor:(UIColor *)textColor
{
    _txtFild.textColor = textColor;
}
-(void)setClearImg:(UIImage *)clearImg
{
    [_ligBtn setImage:clearImg forState:UIControlStateNormal];
}
-(void)setShowClear:(BOOL)showClear
{
    if (showClear == YES) {
        _ligBtn.hidden = NO;
    }else{
        _ligBtn.hidden = YES;
    }
}

-(instancetype)initWithPlaceHolder:(NSString *)placeholder andBackGroudColor:(UIColor *)bkColor
{
    self = [super init];
    if (self) {
        
        self.backgroundColor = bkColor;
        self.layer.cornerRadius = 4;
        self.layer.backgroundColor = bkColor.CGColor;
        
        _txtFild = [[UITextField alloc] init];
        _txtFild.placeholder  = placeholder;
        _txtFild.delegate = self;
//        _txtFild.clearButtonMode = UITextFieldViewModeAlways;
        [self addSubview:_txtFild];
        _txtFild.sd_layout.spaceToSuperView(UIEdgeInsetsMake(0, 10, 0, 10));
        
        _ligBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:_ligBtn];
        _ligBtn.sd_layout.rightSpaceToView(self,10).widthEqualToHeight(self,0.4).heightRatioToView(self,0.4).centerYEqualToView(self);
        [_ligBtn setImage:[UIImage imageNamed:@"login_close"] forState:UIControlStateNormal];
        [_ligBtn addTarget:self action:@selector(clearBtn) forControlEvents:UIControlEventTouchUpInside];
        if (!self.showClear) {
            _ligBtn.hidden = YES;
        }else{
            _ligBtn.hidden = NO;
        }
    }
    return self;
}
-(void)clearBtn
{
    [self.delegate lcsTextClear:self];
}
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents {
    [_txtFild addTarget:target action:action forControlEvents:controlEvents];
}
#pragma mark YSTextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (self.delegate && [self.delegate respondsToSelector:@selector(textField:shouldChangeCharactersInRange:replacementString:)]) {
        return [self.delegate lcsTextField:self shouldChangeCharactersInRange:range replacementString:string];
    }
    return YES;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (self.delegate && [self.delegate respondsToSelector:@selector(lcstextFieldShouldBeginEditing:)]) {
        return [self.delegate lcstextFieldShouldBeginEditing:self];
    }
    return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField {
    if (self.delegate && [self.delegate respondsToSelector:@selector(textFieldShouldClear:)]) {
        return [self.delegate lcsTextFieldShouldClear:self];
    }
    return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    if (self.delegate && [self.delegate respondsToSelector:@selector(lcsTextFieldShouldEndEditing:)]) {
        return [self.delegate lcsTextFieldShouldEndEditing:self];
    }
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (self.delegate && [self.delegate respondsToSelector:@selector(lcsTextFieldShouldReturn:)]) {
        return [self.delegate lcsTextFieldShouldReturn:self];
    }
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    if (self.delegate && [self.delegate respondsToSelector:@selector(lcsTextFieldDidBeginEditing:)]) {
        [self.delegate lcsTextFieldDidBeginEditing:self];
    }
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (self.delegate && [self.delegate respondsToSelector:@selector(lcsTextFieldDidEndEditing:)]) {
        [self.delegate lcsTextFieldDidEndEditing:self];
    }
}

- (BOOL)becomeFirstResponder {
    return [_txtFild becomeFirstResponder];
}
- (BOOL)resignFirstResponder {
    return [_txtFild resignFirstResponder];
}


@end
